package Parser;

import Errors.ErrorLog;
import java.io.*;
import java.util.*;

/*******************************************************************************
 * Reads the contents of an HTML file and creates a stream of characters.
 * @author Tudor Pascu
 * @version FINAL
 *******************************************************************************/
public class ReadFile {

    private BufferedReader reader;
    private File aFile;
    private ArrayList<Character> stream;
    private int currentChar = -1;//has to be -1 for the lexical analyser to look at the first character
    static private ErrorLog errorLog;

    /****************************************************************************
     * Creates a ReadFile object that reads the contents of an HTML file and
     * creates a stream of characters.
     * @param fileToBeRead Address of file to be read.
     ***************************************************************************/
    public ReadFile(String fileToBeRead) {
        aFile = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + fileToBeRead);
        String tempString = "";
        stream = new ArrayList<Character>();
        try {
            reader = new BufferedReader(new FileReader(aFile));
            String line = null;
            while ((line = reader.readLine()) != null) {
                tempString = tempString + " " + line; //This adds a space between each line
            }
            tempString = tempString.substring(1); //This removes the space at the  
        //front of the file, which was added as a result of the above operation.
        } catch (IOException ex) {
            String noPageFound = "<html><font face=\"Verdana\" size=\"30\" color=\"red\">Page not found! Invalid file address.</font></html>";
            stream = new ArrayList<Character>();
            for (int i = 0; i < noPageFound.length(); i++) {
                stream.add(noPageFound.charAt(i));
            }
            return;
        }

        for (int i = 0; i < tempString.length(); i++) {
            if (tempString.charAt(i) != '\t') {
                stream.add(tempString.charAt(i));
            } else {
                stream.add(' ');
            }
        }
        closeFile();
    }

    /****************************************************************************
     * Passes an ErrorLog so that this class has access to the common ErrorLog.
     * @param errors The ErrorLog to be passed in.
     ***************************************************************************/
    static public void setErrorLog(ErrorLog errors) {
        errorLog = errors;
    }

    /****************************************************************************
     * Closes the file and clears the stream array.
     ***************************************************************************/
    public void closeFile() {
        try {
            reader.close();
        } catch (IOException e) {
            errorLog.logError("File couldn't be closed.");
        }
    }

    /****************************************************************************
     * Returns the next character in the stream array.
     * @return Next character in the stream.
     ***************************************************************************/
    public char getCharacter() {
        currentChar++;
        return stream.get(currentChar);
    }

    /****************************************************************************
     * Returns true if there is a next character in the stream array.
     * @return True if there is a next character in the stream array.
     ***************************************************************************/
    public boolean hasCharacter() {
        return ((currentChar + 1) < stream.size());
    }
}